﻿using System;
using ITViet.Web.App_Code;
using ITViet.Domains;
using ITViet.Services;
using System.IO;
using DevExpress.Web.ASPxDataView;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace ITViet.Web.Pages
{
  public partial class NewsPage : BasePage
  {
    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack && !IsCallback)
      {
        LoadNews();
      }
    }

		protected void rptNews_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				News news = e.Item.DataItem as News;
				if (news != null)
				{
					HtmlGenericControl pDate = e.Item.FindControl("pDate") as HtmlGenericControl;
					if (pDate != null)
					{
						pDate.InnerHtml = string.Format(Resources.Strings.LastUpdated, news.PostDate.Value.ToString("dd/MM/yyyy"), news.PostDate.Value.ToString("HH:mm"));
					}

					HtmlImage imgPreview = e.Item.FindControl("imgPreview") as HtmlImage;
					if (imgPreview != null)
					{
						imgPreview.Src = string.IsNullOrEmpty(news.ImagePath) ? string.Format("../{0}", FolderDirector.PreviewImage) : string.Format("../{0}", news.ImagePath);
						imgPreview.Attributes["Onclick"] = string.Format("gotoLocalPage('NewsDetailPage.aspx?t=N&tid={0}&id={1}')", news.CategoryId.ToString(), news.Id.ToString());
					}
				}
			}
		}
    #endregion

    #region Methods
    private void LoadNews()
    {
			NewsService service = new NewsService();
			TList<News> newsList = service.GetByHomeSearch(null, CurrentLanguage.Id, TypeId, null, NewsCategoryType.Normal);
			rptNews.DataSource = newsList;
			rptNews.DataBind();

			if (newsList.Count > 0)
			{
				rptOtherNews.DataSource = service.GetOtherNews(TypeId, CurrentLanguage.Id, newsList[0].Id, 5, NewsCategoryType.Normal);
				rptOtherNews.DataBind();
			}
    }
    #endregion
  }
}
